<?php
/**
 * +----------------------------------------------------------------------
 * | 模型
 * +----------------------------------------------------------------------
 *                      _ooOoo_
 *                     o8888888o            | AUTHOR: 杜云
 *                     88" . "88            | EMAIL: 987772927@qq.com
 *                     (| -_- |)            | QQ: 987772927
 *                     O\  =  /O            | WECHAT: 987772927
 *                  ____/`---'\____
 *                .'  \\|     |//  `.
 *               /  \\|||  :  |||//  \
 *              /  _||||| -:- |||||-  \
 *              |   | \\\  -  /// |   |
 *              | \_|  ''\-/''  |   |
 *              \  .-\__  `-`  ___/-. /
 *            ___`. .'  /-.-\  `. . __
 *         ."" '<  `.___\_<|>_/___.'  >'"".
 *        | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *        \  \ `-.   \_ __\ /__ _/   .-` /  /
 *   ======`-.____`-.___\_____/___.-`____.-'======
 *                      `=-='
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);
namespace App\Model;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Database\Model\SoftDeletes;
/**
 * @property int $id
 * @property string $name
 * @property string $code
 * @property int $sort
 * @property string $remark
 * @property int $created_user
 * @property \Carbon\Carbon $created_at
 * @property string $update_at
 * @property string $deleted_at
 */
class ConfigCate extends BaseModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'config_cates';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name','code','sort','remark','config', 'field_list','created_at','update_at'];
    protected $hidden = ['deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'sort' => 'integer', 'created_at' => 'datetime'];
     /**
     * @param  string  $value
     * @return void
     */
    public function setFieldListAttribute($value)
    {
        $this->attributes['field_list'] = json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    /**
     * @param  string  $value
     * @return void
     */
    public function setConfigAttribute($value)
    {
        $this->attributes['config'] = json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    /**
     * @param  string  $value
     * @return void
     */
    public function getFieldListAttribute($value)
    {
        return !empty($value)?json_decode($value,true):'';
    }
    /**
     * @param  string  $value
     * @return void
     */
    public function getConfigAttribute($value)
    {
        return !empty($value)?json_decode($value,true):'';
    }
    public function configs()
    {
        return $this->hasMany(Config::class,'cate_id', 'id');
    }
    /**
     * [getConfigs description]
     * @return array 配置数组
     */
    public static function getConfigs()
    {
        $list = self::orderBy('sort','asc')->orderBy('id','asc')->get();
        $list->map(function($item){
            $newArr = $item->configs()->pluck('value','name')->toArray();
            $data = [];
            foreach (($item['field_list']?:[]) as $key => $value) {
                $v = $newArr[$value['__vModel__']]??'';
                if(is_numeric($v)) $v = (int)$v;
                $value['__config__']['defaultValue'] = $v;
                $data[] = $value;
            }
            $item['field_list'] = $data;
        });
        return $list;
    }
}